package com.pan.controller.tools;

import com.pan.domain.User;
import com.pan.service.IUserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;

//验证用户权限的方法
public class Methods {
    //管理员检查
    public Boolean isManager(HttpServletRequest request, IUserService userService){
        //检查用户权限
        HttpSession session = request.getSession();
        User user1 = (User) session.getAttribute("user");
        //先查询出包含部门编号的所有信息
        User user2 = userService.getByUsername(user1.getUsername());
        //然后通过部门编号查询部门权限
        String aty = userService.getByDeptno(user2.getDeptno());

        if (!aty.equals("全部")){
            return false;
        }
        return true;
    }

    //财务人员检查
    public Boolean isCaiWu(HttpServletRequest request, IUserService userService){
        //检查用户权限
        HttpSession session = request.getSession();
        User user1 = (User) session.getAttribute("user");
        //先查询出包含部门编号的所有信息
        User user2 = userService.getByUsername(user1.getUsername());
        //然后通过部门编号查询部门权限
        String aty = userService.getByDeptno(user2.getDeptno());

        if (!aty.equals("财务")){
            return false;
        }
        return true;
    }

    //人事人员或管理员检查
    public Boolean isRenShiOrManager(HttpServletRequest request, IUserService userService){
        //检查用户权限
        HttpSession session = request.getSession();
        User user1 = (User) session.getAttribute("user");
        //先查询出包含部门编号的所有信息
        User user2 = userService.getByUsername(user1.getUsername());
        //然后通过部门编号查询部门权限
        String aty = userService.getByDeptno(user2.getDeptno());

        if (!aty.equals("全部")&&!aty.equals("人事")){
            return false;
        }
        return true;
    }

    //人事人员检查
    public Boolean isRenShi(HttpServletRequest request, IUserService userService){
        //检查用户权限
        HttpSession session = request.getSession();
        User user1 = (User) session.getAttribute("user");
        //先查询出包含部门编号的所有信息
        User user2 = userService.getByUsername(user1.getUsername());
        //然后通过部门编号查询部门权限
        String aty = userService.getByDeptno(user2.getDeptno());

        if (!aty.equals("人事")){
            return false;
        }
        return true;
    }

    //研发人员检查
    public Boolean isYanfa(HttpServletRequest request, IUserService userService){
        //检查用户权限
        HttpSession session = request.getSession();
        User user1 = (User) session.getAttribute("user");
        //先查询出包含部门编号的所有信息
        User user2 = userService.getByUsername(user1.getUsername());
        //然后通过部门编号查询部门权限
        String aty = userService.getByDeptno(user2.getDeptno());

        if (!aty.equals("研发")){
            return false;
        }
        return true;
    }

    //检查是否为用户本人


    //获取用户真实ip地址
    public String getUserIp(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    try {
                        ipAddress = InetAddress.getLocalHost().getHostAddress();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                }
            }
            // 通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null) {
                if (ipAddress.contains(",")) {
                    return ipAddress.split(",")[0];
                } else {
                    return ipAddress;
                }
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
